<template>
    <div class="alert">
        <div class="alert-box">
            <i class="el-icon-close close" @click="cancel" style="color: white;z-index:10;"></i>
	    <div class="bt">物品列表</div>
	    <div class="xxx" style="float: left;width: 20%;height:100px;">
	    	<div class="wpfl" style="text-align: center;font-size: 16px;background: #C0CCDA;color: white;line-height: 30px;">物品分类</div>
	    	<div v-for="v in comboxlist" :class="v.classname">
	    	       <div class="jclt" @click="wpbtnclick(v.id)">{{v.mc}}</div>
	    	       <!--<el-select v-model="vwpflid" class="input">
						<el-option :label="v.mc" :value="v.id" v-for="v in comboxlist"></el-option>
					</el-select>-->
			</div>
	    </div>
            <div class="left">
                <div class="search">
                <el-input placeholder="请输入名称检索" class="input" v-model="vkeyword"  @keyup.enter.native="search" icon="el-icon-search"></el-input>
                    <el-button type="primary" class="btn" @click="search">搜索</el-button>
                    <el-button type="success" class="add btn" @click="submit" >确定</el-button>
                </div>

                <!-- 数据表格展示区域 -->
                <el-table
                    :data="dataList"
                    ref="multipleTable"
                    border
                    show-summary
                    stripe
                    align="center"
                    class="admin-table"
                    :row-class-name="tableRowClassName"
                    row-key="id"
                    @select="checkSelect"
                    @select-all="checkall">

                    <el-table-column
                        type="selection"
                        width="55"
                        :reserve-selection="true"></el-table-column>


                    <el-table-column
                        :prop="v.prop"
                        :label="v.label"
                        v-for="(v,index) in portTitleList"
                        align="center"
                        show-overflow-tooltip
                        v-if="v.hide != 0"
                        :formatter="v.formatter"
                        :align="v.align || 'center'"
                        :fixed="v.fixed"
                        :class-name="v.columnStyle"
                        :width="v.width"
                        :min-width="v.minWidth"
                        :header-align="v.headerAlign || 'center'"
                        >
                        <template scope="scope">
                            <div slot="reference" :style="tableCellControl(scope)">
                                {{ scope.row[scope.column.property] }}
                            </div>
                         </template>
                    </el-table-column>
                </el-table>

                <!-- 分页查询区域 -->
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page.sync="pageindex"
                    :page-sizes="pagesizes"
                    :page-size="pagesize"
                    :layout="layout"
                    :total="total"
                    class="class-page">
                </el-pagination>
            </div>

            <div class="right">
                <div class="choose">
                    <ul class="header">
                        <li class="first">
                            <div>
                                <span>已选择:</span>
                                <span class="number">{{multipleList.length}}</span>
                            </div>
                            <span class="del el-icon-delete" @click="del()">
                                <i></i>
                            </span>
                        </li>
                        <li v-for="(v,index) in multipleList">
                            <span class="name ellipsis">{{v.mc}}</span>
                            <span class="del el-icon-delete" @click="del(multipleList[index])">
                                <i></i>
                            </span>
                        </li>
                    </ul>
                </div>
                <!-- <el-button @click="toggleSelection(multipleList)">asdasdsadas</el-button> -->
            </div>

            <!-- 动画加载 -->
            <LoadingComponent v-show="isLoading"></LoadingComponent>
        </div>
    </div>
</template>

<script>
    import { mapState } from 'vuex'
    import LoadingComponent from 'components/loading'
import qs from 'qs'
    export default {
        components: {
            LoadingComponent
        },
        data () {
            return {
                vwpflid:'',
                vkeyword:'',
                multipleList: [],
                 comboxlist:[],
                dataList: [],  //表格数据
                vselectwpid: '',

                /*
                    prop :  接口参数名
                    label : 接口中文名
                    hide : 是否隐藏这一列
                    align : 这一列对齐顺序 ： left,right,center
                    headerAlign : 这一列表头对齐方式 ： left,right,center
                */
                portTitleList: [
                    {
                        prop:'id',
                        label:'ID',
                        hide:0,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center',
                        formatter: function(row,column) {}
                    },
                    {
                        prop:'wpflid',
                        label:'物品分类id-T_wpfl.id',
                        hide:0,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center',
                        formatter: function(row,column) {}
                    },
                    {

                        prop:'mc',
                        label:'名称',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center',
                        formatter: function(row,column) {}
                    },
                    {
                        prop:'kcsl',
                        label:'库存数量',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center',
                        formatter: function(row,column) {}
                    },
                    {
                        prop:'jg',
                        label:'采购价格',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center',
                        formatter: function(row,column) {}
                    },
                    {
                        prop:'dw',
                        label:'单位',
                        hide:1,
                        align:'center',
                        fixed:false,
                        width:'',
                        minWidth:'',
                        headerAlign:'center',
                        formatter: function(row,column) {}
                    }

                ],

                /*动画加载*/
                isLoading: true,//动画加载
                searchLoading: false,//搜索按钮触发按钮动画

                /*模糊搜索条件*/
                inputmc:'' , //  班级名称,

                /*分页属性设置*/
                layout: 'total, sizes, prev, pager, next, jumper',//组件布局
                pageindex: 1,//当前分页页码
                pagesizes: [12, 24, 36, 72],//显示个数的选择器选项
                pagesize: 12,//每页显示条目数
                total: 0,//总条数

                dropDownData:'', //
            }
        },
        created () {

            //加载页面时调用
            this.search();//分页获取默认数据
            this.getcomboxlist();
            this.isLoading = true;//动画开始

        },
        mounted () {

        },
        methods: {
        	wpbtnclick(wpid) {
				this.vselectwpid = wpid;
				for(let i in this.comboxlist) {
						this.comboxlist[i].classname = 'jc';
						if(this.comboxlist[i].id==this.vselectwpid){
//							alert(this.vselectwpfxid);
						this.comboxlist[i].classname = 'jc2';
						}
				}
				this.search();
			},
        	 getcomboxlist(){
				var logininfo = JSON.parse(localStorage.logininfo) ;
        var params3 = qs.stringify({
          keyword: this.vkeyword, // keyword
          wpflid: this.vwpflid, // 物品分类id
          yhid:logininfo.id,
          xxid:logininfo.xxid,
          pageindex: this.pageindex,
          pagesize: this.pagesize
			 });
					this.searchLoading = true;
					this.axios.post(this.$store.state.api + 'wpfl/list',params3)
          .then( res => {
							this.flid=res.data.data.list[0].id;
					this.wpbtnclick(this.flid);
					let data = res.data.data;
					let list = data.list;
					for(let i in list) {
						list[i].classname = 'jc';
						if(list[i].id==this.vselectwpid){
						list[i].classname = 'jc2';
						}
					}
					this.comboxlist = list;
					console.log("list",this.comboxlist)
					}).catch( res => {
							console.log(res)
							this.searchLoading = false;
					})
			},
            tableRowClassName (row, index) {//隔行变色颜色设置
                /*if ( index%4 == 0) {
                    return "table-cell-green";
                }else if( index%4 == 1 ){
                    return "table-cell-yellow";
                }else if( index%4 == 2 ){
                    return "table-cell-red";
                }else{
                    return "table-cell-blue"
                }*/
            },
            tableCellControl (scope) {//表格单元格控制CSS样式
                /*if (scope.row.id == 112 && scope.column.property == "iname") {
                    return {
                        color:"red",
                    }
                }else{
                    return {
                        color:"green",
                    }
                }*/
            },
            // selectable (row, index) {
            //     console.log(row,index)
            //     return true
            // },

            search () {//查询搜索触发事件
//alert(this.vselectwpid)
                var logininfo = JSON.parse(localStorage.logininfo) ;//获取学校  用户id
                var params3 = qs.stringify({
                  yhid:logininfo.id,
                  xxid:logininfo.xxid,
                  keyword: this.vkeyword, // keyword
                  wpflid: this.vselectwpid, // 物品分类id
                  pageindex: this.pageindex,
                  pagesize: this.pagesize
                });
                this.searchLoading = true;
                this.axios.post(this.$store.state.api + 'wpfx/list',params3)
                .then( res => {
                    console.log(res)
                    this.isLoading = false;//动画结束
                    let data = res.data.data;
                    let list = data.list;
                    this.total = data.totalPage;
                    this.dataList = list;
                    this.searchLoading = false;
                }).catch( res => {
                    console.log(res)
                    this.searchLoading = false;
                })
            },
           submit() {
//				this.$store.state.loading = true;
//				var logininfo = JSON.parse(localStorage.logininfo);
//				var params3 = qs.stringify({
//					wpids: this.wpid,
//					note:'',
//					yhid: logininfo.id,
//					xxid:logininfo.xxid
//				});
//				this.axios.post(this.$store.state.api + 'wppdmx/insert', params3)
//					.then(res => {
//						if(res.data.code == 1) {
//							this.$emit('close', { type: 'add', cancel: false });
//							this.$store.state.loading = false;
//						} else {
//							this.$emit('close', { type: 'add', cancel: false });
//							this.$store.state.loading = false;
//						}
//					}).catch(res => {
//						this.$message.warning("服务器繁忙，请重试");
//						this.$store.state.loading = false;
//					})
               let arr = [];
                this.multipleList.forEach(function(v, i){
//                     console.log('1111',v)
                    arr.push({
                        id:v.id,
                        wpflid:v.wpflid,
                        type:'物品',
                        mc:v.mc,
                        dw: '套',
//                      zk: '100',
                        number:1,
                        jg:v.jg,
                        zj:v.zj
                    })
                })
                this.multipleList = arr;
                this.$emit('close',{type:'add',cancel:false,data:this.multipleList})

                console.log(123)
                console.log(this.multipleList)
			},

            del (obj) {
                if (obj) {
                    // console.log(obj)
                    this.$refs.multipleTable.toggleRowSelection(obj,false);
                }else{
                    this.$refs.multipleTable.clearSelection();
                    this.multipleList = [];
                }

            },

            handleSizeChange (val) {//每页显示条数触发事件
                this.pagesize = val;
                this.pageindex = 1;
                this.search();
            },

            handleCurrentChange (val) {//分页触发事件
                this.search();
            },
            checkSelect (selection, row) {//点击单选select触发事件
                // console.log(selection, row)
                this.multipleList = selection;
                console.log(this.multipleList)
                // this.multipleList.push({
                //     id:
                // })
                // let strID = [];
                // selection.forEach(function(v,i){
                //     strID.push(v.id)
                // })
                // this.multipleDelNum = strID.join(",");
            },
            checkall (selection) {//点击全选select触发事件
                // console.log(selection)
                this.multipleList = selection;
                console.log(this.multipleList)
                // let strID = [];

                // selection.forEach(function(v,i){
                //     strID.push(v.id)
                // })
                // // console.log(strID.join(","))
                // this.multipleDelNum = strID.join(",");
                // // console.log(strID)
            },

            cancel () {
               this.$emit('close',{type:'add',cancel:true})
            },

            /* 组件的弹框  */
            componentsShow (type,data) { //显示组件
                console.log(type,data)
                for ( let i in this.componentList ) {
                    if (type == this.componentList[i].type) {

                        this.componentList[i].toggle = true;
                        this.componentList[i].params = data;

                        if (this.componentList[i].data.type == 'single') {
                            this.componentList[i].data.index = data.row.id

                        }else if(this.componentList[i].data.type == 'multiple'){
                            this.componentList[i].data.index = this.multipleDelNum;

                        }else{
                            return;
                        }
                        return;
                    }
                }
            },
            componentsHide (setting) { //关闭组件
                for ( let i in this.componentList ) {
                    if (setting.type == this.componentList[i].type) {
                        this.componentList[i].toggle = false;

                        if (!setting.cancel) {
                           this.search();
                        }
                        return;
                    }
                }
            }

        },
        computed: {
            ...mapState([
                'api'//域名
            ])
        }
    }
</script>

<style lang="less" scoped>

    .alert-box {
        text-align: left;
        min-width:891px;
        height: 80%;
    }
    .bt{
			width: 104.8%;
			height: 50px;
			margin: 0 auto;
			margin-top: -20px;
			background: #50BFFF;
			margin-left: -20px;
			margin-bottom: 20px;
			color:white;
			text-align:left;
			line-height: 50px;
			font-size: 17px;
			padding-left: 15px;
		}
		.jc {
			width: 100%;
			height: 50px;
			float: left;
			background: white;
			border-bottom: 1px solid #CCCCCC;
		}
		.jc2{
			width: 100%;
			height: 50px;
			float: left;
			background: red;
			border-bottom: 1px solid #CCCCCC;
		}
		.jc:hover {
			background: #C0CCDA;
		}
		.jclt {
			width: 100%;
			height: 50px;
			float: left;
			font-size: 16px;
			text-align: center;
			line-height: 50px;
			/*border-right: 1px solid #CCCCCC;*/
			cursor: pointer;
		}

    .left, .right {
        display: inline-block;
        vertical-align: top;
    }
    .left{
        width: 30%;
        min-width:440px;
        float: left;
    }
    .right {
        width: 25%;
        max-height:500px;
        float: right;
        overflow: auto;

        .choose {
            box-shadow:0px 0px 10px #969696;
            border-radius:10px;
        }
        .header{

            li{
                display:flex;
                align-items:center;
                justify-content: space-between;
                padding:10px 20px;

                .del {
                    cursor: pointer;
                    position: relative;

                    &:hover i {
                        position: absolute;
                        left:50%;top:50%;
                        transform: translate(-50%, -50%);
                        width: 25px;
                        height: 25px;
                        border-radius: 50%;
                        background: rgba(0,0,0,0.2);
                    }
                }

            }

            .first {
                background: #EEF1F4;

                .number {
                    min-width: 10px;
                    padding:2px 7px;
                    font-weight: 700;
                    border-radius:10px;
                    background: #FF4949;
                    color: #fff;
                }
            }

        }
    }
    .input {
        width:150px;
        margin-left: 3px;
    }
    .btn {
        width:80px;
        margin-left:20px;
        padding:7px 15px;
    }

    .admin-table {
        margin-top:10px;
    }

    h2{
        line-height: 30px;
        text-align: center;
    		font-weight: 700;
    		margin-bottom:  20px;
    }

    .class-page {
        margin-top:10px;
    }
</style>
